package com.cuanbo.dualscreenteachfu.bean;


import com.cuanbo.dualscreenteachfu.matrix.MatrixHeartbeatThread;
import com.cuanbo.dualscreenteachfu.utils.LogUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 发送消息队列
 */
public class DataList {
    private static List<DataBean> sendList = new ArrayList<DataBean>();
    public static Object lock = new Object();

    /**
     * 往消息队列中添加消息
     *
     * @param bean 要发送消息的封装类
     */
    public static void add(DataBean bean) {
        /*if (!bean.getData().equals("#JSON:weburl,0.")) {
            //LogUtil.add("cmd", "往要执行的指令集合中插入:" + bean.getData() + " 延迟执行时间(毫秒): " + bean.getDelay(), "CmdProcessLog");
        }*/
        synchronized (DataList.lock) {
            bean.setTime(bean.getDelay() + new Date().getTime());
            sendList.add(bean);
        }
    }

    /**
     * 往消息队列中添加消息
     *
     * @param cmd 要发送的消息
     */
    public static void add(String cmd) {
        /*if (!cmd.equals("#JSON:weburl,0.")) {
            //LogUtil.add("cmd", "往要执行的指令集合中插入:" + cmd, "CmdProcessLog");
        }*/
        synchronized (DataList.lock) {
            sendList.add(new DataBean(cmd));
        }
    }

    /**
     * 从消息队列中取消息
     */
    public static String get() {
        synchronized (DataList.lock) {
            String strData = null;
            for (int i = 0; i < sendList.size(); i++) {
                DataBean cmdBean = sendList.get(i);
                if (cmdBean.getTime() - new Date().getTime() <= 0) {//当前时间-执行时间<=0
                    strData = cmdBean.getData();
                    sendList.remove(cmdBean);
                    return strData;
                }
            }
            return strData;
        }
    }

    public static void clear() {
        synchronized (DataList.lock) {
            sendList.removeAll(sendList);
        }
    }

    public static void remove(String cmd) {
        synchronized (DataList.lock) {
            DataBean removeBean = null;
            for (int i = 0; i < sendList.size(); i++) {
                DataBean dataBean = sendList.get(i);
                if (dataBean.getData().contains(cmd)) {
                    removeBean = dataBean;
                    break;
                }
            }
            if (removeBean != null) {
                sendList.remove(removeBean);
                LogUtil.add("提示", "移除指令->" + cmd, "矩阵");
            }
        }
    }

    /**
     * 集合中是否包含心跳包
     */
    public static boolean containHeartbeatPackage() {
        synchronized (DataList.lock) {
            for (int i = 0; i < sendList.size(); i++) {
                DataBean dataBean = sendList.get(i);
                if (dataBean.getData().equals(MatrixHeartbeatThread.heartbeat)) {
                    return true;
                }
            }
            return false;
        }
    }

    /**
     * 集合中是否包含传感器查询指令
     */
    public static boolean containSensorDataSend() {
        synchronized (DataList.lock) {
            for (int i = 0; i < sendList.size(); i++) {
                DataBean dataBean = sendList.get(i);
                if (dataBean.getData().contains("查询温湿度")) {
                    return true;
                }
            }
            return false;
        }
    }

    /**
     * 集合中是否包含人体传感器查询指令
     */
    public static boolean containLBSensorDataSend() {
        synchronized (DataList.lock) {
            for (int i = 0; i < sendList.size(); i++) {
                DataBean dataBean = sendList.get(i);
                if (dataBean.getData().equals(">COM-CPC-BAUD:5,6,%H51 FF FF 06 00 03 58.")) {
                    return true;
                }
            }
            return false;
        }
    }
}
